using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections;
using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Net.Sockets;
using Mud.EmlenMudMain;
using BrainTechLLC.ThreadSafeObjects;

namespace BrainTechLLC
{
    [Serializable]
    public class OldSchoolRooms //: MudThing, OldSchoolRooms
    {

        public OldSchoolRooms() { }


        public void fread_room_old_format(StreamReader sr, int vnum, AreaData area)
        {
            char[] spc = " ".ToCharArray();
            RoomData room = new RoomData();

            room.area = area;
            room.VnumRoom = vnum;

            char[] delim = new char[3];
            delim[0] = ' ';
            delim[1] = '\r';
            delim[2] = '\n';

            while (sr.EndOfStream == false)
            {
                string szLine = sr.ReadLine().Trim();
                string[] rgsz = szLine.Split(delim, 2, StringSplitOptions.RemoveEmptyEntries);

                if (rgsz.Length > 0)
                {
                    string szWord = rgsz[0];
                    if (szWord == "End")
                    {
                        if (room.RoomDescription != null && room.RoomDescription.Length > 2)
                        {
                            area.NumberOfDescribedRooms++;
                        }
                        area.NumberOfRoomsInArea++;
                        EmlenMud.WorldTemplates.htRooms.Add(room.VnumRoom, room);
                        area.RoomsInArea.Add(room);
                        break;
                    }


                    if (szWord == "ExtraDescr")
                    {
                        ExtraDescriptionData ed = new ExtraDescriptionData();
                        if (room.MoreRoomInfo == null) room.MoreRoomInfo = new DynamicRoomDetails();
                        ed.Keyword = rgsz[1].TrimEnd('~');
                        ed.Description = "";
                        string szL = sr.ReadLine();
                        while (szL.Trim() != "~")
                        {
                            ed.Description += szL + "\n\r";
                            szL = sr.ReadLine();
                        }
                        room.ExtraRoomDescriptions.Add(ed);
                        EmlenMud.top_ed++;
                        continue;
                    }

                    if (szWord == "Reset")
                    {
                        ResetData pReset = new ResetData();
                        if (room.MoreRoomInfo == null) room.MoreRoomInfo = new DynamicRoomDetails();
                        string[] rgspl2 = rgsz[1].Split(spc, StringSplitOptions.RemoveEmptyEntries);
                        pReset.Command = (char)((rgspl2[0])[0]);
                        pReset.Rs_vnum = Convert.ToInt32(rgspl2[1]);
                        pReset.Loc = Convert.ToInt32(rgspl2[2]);
                        pReset.Percent = Convert.ToInt32(rgspl2[3]);

                        room.Resets.Add(pReset);
                        continue;
                    }


                    if (szWord == "Descr")
                    {
                        string szL2 = sr.ReadLine();
                        room.RoomDescription = "";
                        while (szL2.Trim() != "~")
                        {
                            room.RoomDescription += szL2;// +"\n\r";
                            szL2 = sr.ReadLine();
                        }
                        continue;
                    }

                    if (szWord == "Door")
                    {
                        ExitData pexit = new ExitData();
                        string[] rgspl3 = rgsz[1].Split(spc, StringSplitOptions.RemoveEmptyEntries);
                        int door = Convert.ToInt32(rgspl3[0]);
                        if (door < 0 || door > 5)
                        {
                            Debug.WriteLine("Fread_rooms: bad door number " + door.ToString() + " on room " + room.VnumRoom.ToString());
#if NO_SILVERLIGHT
                            GlobalLogger.Log("Error", "Fread_rooms: bad door number " + door.ToString() + " on room " + room.VnumRoom.ToString(), LogSeverity.Critical);
#endif

                            continue;
                        }
                        int tmp = Convert.ToInt32(rgspl3[1]);
                        if (tmp != 0)
                        {
                            if (pexit.D_info == null) pexit.D_info = new DoorData();
                            pexit.D_info.Rs_flags = (ExitFlags)tmp;
                        }
                        tmp = Convert.ToInt32(rgspl3[2]);
                        if (tmp != 0)
                        {
                            if (pexit.D_info == null) pexit.D_info = new DoorData();
                            pexit.D_info.Key = tmp;
                        }
                        pexit.Vnum = Convert.ToInt32(rgspl3[3]);
                        int tmp2 = Convert.ToInt32(rgspl3[4]);
                        int tmp3 = Convert.ToInt32(rgspl3[5]);
                        if (tmp != 0 && tmp != 30)
                        {
                            if (pexit.D_info == null) pexit.D_info = new DoorData();
                            pexit.D_info.Maxstr = tmp2;	// Check ordering here
                            pexit.D_info.Str = tmp3;
                        }
                        string szL4 = sr.ReadLine();
                        while (szL4.Trim() != "~")
                        {
                            if (pexit.D_info == null) pexit.D_info = new DoorData();
                            pexit.D_info.Description += szL4 + "\n\r";
                            szL4 = sr.ReadLine();
                        }

                        string szL5 = sr.ReadLine().Trim().TrimEnd('~');
                        if (pexit.D_info == null) pexit.D_info = new DoorData();
                        pexit.D_info.Keyword = szL5;
                        room.Exits[door] = pexit;
                        continue;
                    }


                    //KEY ("NJAO", currentRoom.img[0], fread_number(fp));
                    //KEY ("NJAT", currentRoom.img[1], fread_number(fp));
                    //KEY ("NAP", currentRoom.a, fread_number (fp));
                    //KEY ("NAC", currentRoom.c, fread_number (fp));
                    if (szWord == "Name")
                    {
                        room.Name = new NameAndDescription() { Name = rgsz[1].TrimEnd('~') };
                        continue;
                    }
                    if (szWord == "NTPS")
                    {
                        int t = Convert.ToInt32(rgsz[1]);
                        room.TravelPoints = t;
                        continue;
                    }

                    if (room.MoreRoomInfo == null) room.MoreRoomInfo = new DynamicRoomDetails();

                    if (szWord == "Mlt")
                    {
                        continue;
                    }
                    if (szWord == "Mor")
                    {
                        continue;
                    }
                    if (szWord == "Mcts")
                    {
                        continue;
                    }

                    if (szWord == "Move_dir")
                    {
                        room.AutoMoveDirection = (GeneralDirection)Convert.ToInt32(rgsz[1]);
                        continue;
                    }

                    if (szWord == "Movemess")
                    {
                        string szL5 = sr.ReadLine();
                        room.Move_message = szL5.Trim().TrimEnd('~');
                        while (szL5.Trim() != "~" && szL5.Trim().EndsWith("~") == false)
                        {
                            room.Move_message += szL5 + "\n\r";
                            szL5 = sr.ReadLine();
                        }
                        continue;
                    }
                    if (szWord == "Mdescript")
                    {
                        string szL5 = sr.ReadLine();
                        while (szL5.Trim() != "~")
                        {
                            szL5 = sr.ReadLine();
                        }
                        continue;
                    }


                    if (szWord == "Flags") { room.RoomFlagsSet = (RoomFlags)Convert.ToInt32(rgsz[1]); continue; }
                    if (szWord == "Flags2")
                    {
                        Int64 i64 = Convert.ToInt64(rgsz[1]);
                        Int64 i64Sh = i64 << 32;
                        room.RoomFlagsSet |= (RoomFlags)i64Sh;
                        continue;
                    }

                    if (szWord == "Shd")
                    {
                        room.RoomFlagsSet |= (Convert.ToInt32(rgsz[1]) == 1) ? RoomFlags.IS_SHADED : 0;
                        continue;
                    }
                    if (szWord == "Sector")
                    {
                        room.SectorTypeSet = (SectorType)Convert.ToInt32(rgsz[1]);
                        continue;
                    }
                }
            }
        }

        public void WriteOldRoomFormat(System.IO.StreamWriter sw, int vnum, RoomData room, AreaData area)
        {
            char[] delim = new char[3];
            delim[0] = ' ';
            delim[1] = '\r';
            delim[2] = '\n';

            sw.WriteLine("#" + vnum.ToString());
            sw.WriteLine("Name " + room.ShortName + "~");
            sw.WriteLine("Descr");
            sw.WriteLine(room.RoomDescription);
            sw.WriteLine("~");

            if (room.ExtraRoomDescriptions.Count > 0)
            {
                for (int n = 0; n < room.ExtraRoomDescriptions.Count; n++)
                {
                    ExtraDescriptionData ed = (ExtraDescriptionData)room.ExtraRoomDescriptions[n];
                    sw.WriteLine("ExtraDescr " + ed.Keyword + "~");
                    sw.WriteLine(ed.Description);
                    sw.WriteLine("~");
                }
            }

            if (room.TravelPoints > 0) sw.WriteLine("NTPS " + room.TravelPoints.ToString());
            if (room.AutoMoveDirection > 0) sw.WriteLine("Move_dir " + room.AutoMoveDirection.ToString());
            if (!string.IsNullOrEmpty(room.Move_message))
            {
                sw.WriteLine("Movemess");
                sw.WriteLine(room.Move_message + "~");
            }
            Int64 flags = ((((Int64)room.RoomFlagsSet) << 32) >> 32);
            Int64 flags2 = (((Int64)room.RoomFlagsSet) >> 32);
            sw.WriteLine("Flags " + flags.ToString());
            sw.WriteLine("Flags2 " + flags2.ToString());
            if ((room.RoomFlagsSet & RoomFlags.IS_SHADED) != 0) sw.WriteLine("Shd 1");
            sw.WriteLine("Sector " + ((int)room.SectorTypeSet).ToString());

            for (int n = 0; n < room.Resets.Count; n++)
            {
                ResetData reset = (ResetData)room.Resets[n];
                sw.WriteLine("Reset " + reset.Command.ToString() + " " + reset.Rs_vnum.ToString() + " " +
                  reset.Loc.ToString() + " " + reset.Percent.ToString());
            }

            for (int n = 0; n < (int)GeneralDirection.Max; n++)
            {
                ExitData ex = (ExitData)room.Exits[n];
                if (ex != null)
                {
                    if (ex.D_info != null)
                    {
                        sw.WriteLine("Door " + n.ToString() + " " + ((int)ex.D_info.Rs_flags).ToString() + " " +
                          ex.D_info.Key.ToString() + " " + ex.Vnum.ToString() + " " +
                          ex.D_info.Maxstr.ToString() + " " + ex.D_info.Str.ToString());

                        if (!string.IsNullOrEmpty(ex.D_info.Description))
                            sw.WriteLine(ex.D_info.Description);

                        sw.WriteLine("~");

                        sw.WriteLine(ex.D_info.Key + "~");
                    }
                }
            }

            sw.WriteLine("End");
            sw.WriteLine("");
        }

        public void load_rooms_old_format(StreamReader sr, AreaData area)
        {

            area.NumberOfRoomsInArea = 0;
            while (sr.EndOfStream == false)
            {
                char letter = ' ';
                while (letter != '#' && sr.EndOfStream == false) letter = (char)sr.Read();
                if (letter != '#')
                {
                    Debug.WriteLine("Error reading rooms: No # found in area " + area.ShortName);
                    EmlenMud.LogError("Error reading rooms: No # found in area " + area.ShortName, null, LogType.WorldError);
                    return;
                }
                string szVnum = sr.ReadLine().Trim();
                int vnum = Convert.ToInt32(szVnum);
                if (vnum == 0)	// 0 signals we are done
                    break;


                if (EmlenMud.WorldTemplates.htRooms.ContainsKey(vnum) == true)
                {
                    EmlenMud.LogError("Load_rooms: vnum " + szVnum + " duplicated in area " + area.ShortName, null, LogType.WorldError);
                    continue;
                }

                fread_room_old_format(sr, vnum, area);
                area.NumberOfRoomsInArea++;
                if (area.OpenToPlayers != 0) EmlenMud.WorldTemplates.nRoomsOpen++;
                if (vnum > EmlenMud.WorldTemplates.top_room_vnum) EmlenMud.WorldTemplates.top_room_vnum = vnum;
            }
        }

        public void save_rooms_old_format(System.IO.StreamWriter sw, AreaData area)
        {
            // Do nothing.. this is done in the area file
        }

    }
}
//namespace Mud.EmlenMudMain
//{
//  [Serializable]
//  public class OldSchoolRooms : MudThing, OldSchoolRooms
//  {

//    public OldSchoolRooms() : base(EmlenMud) { }


//    public void fread_room_old_format(StreamReader sr, int vnum, AreaData area)
//    {
//      char[] spc = " ".ToCharArray();
//      RoomData room = new RoomData(EmlenMud);

//      room.area = area;
//      room.VnumRoom = vnum;

//      char[] delim = new char[3];
//      delim[0] = ' ';
//      delim[1] = '\r';
//      delim[2] = '\n';

//      while (sr.EndOfStream == false)
//      {
//        string szLine = sr.ReadLine().Trim();
//        string[] rgsz = szLine.Split(delim, 2, StringSplitOptions.RemoveEmptyEntries);

//        if (rgsz.Length > 0)
//        {
//          string szWord = rgsz[0];
//          if (szWord == "End")
//          {
//            if (room.RoomDescription != null && room.RoomDescription.Length > 2)
//            {
//              area.NumberOfDescribedRooms++;
//            }
//            if (EmlenMud != null)
//            {
//              EmlenMud.WorldTemplates.htRooms.Add(room.VnumRoom, room);
//            }
//            area.NumberOfRoomsInArea++;
//            area.RoomsInArea.Add(room);
//            break;
//          }


//          if (szWord == "ExtraDescr")
//          {
//            ExtraDescriptionData ed = new ExtraDescriptionData(room.EmlenMud);
//            if (room.MoreRoomInfo == null) room.MoreRoomInfo = new DynamicRoomDetails();
//            ed.Keyword = rgsz[1].TrimEnd('~');
//            ed.Description = "";
//            string szL = sr.ReadLine();
//            while (szL.Trim() != "~")
//            {
//              ed.Description += szL + "\n\r";
//              szL = sr.ReadLine();
//            }
//            room.ExtraRoomDescriptions.Add(ed);
//            continue;
//            // EmlenMud.em.top_ed++;				
//          }

//          if (szWord == "Reset")
//          {
//            ResetData pReset = new ResetData(EmlenMud);
//            if (room.MoreRoomInfo == null) room.MoreRoomInfo = new DynamicRoomDetails();
//            string[] rgspl2 = rgsz[1].Split(spc, StringSplitOptions.RemoveEmptyEntries);
//            pReset.Command = (char)((rgspl2[0])[0]);
//            pReset.Rs_vnum = Convert.ToInt32(rgspl2[1]);
//            pReset.Loc = Convert.ToInt32(rgspl2[2]);
//            pReset.Percent = Convert.ToInt32(rgspl2[3]);

//            room.Resets.Add(pReset);
//            continue;
//          }


//          if (szWord == "Descr")
//          {
//            string szL2 = sr.ReadLine();
//            room.RoomDescription = "";
//            while (szL2.Trim() != "~")
//            {
//              room.RoomDescription += szL2 + "\n\r";
//              szL2 = sr.ReadLine();
//            }
//            continue;
//          }

//          if (szWord == "Door")
//          {
//            ExitData pexit = new ExitData(EmlenMud);
//            string[] rgspl3 = rgsz[1].Split(spc, StringSplitOptions.RemoveEmptyEntries);
//            int door = Convert.ToInt32(rgspl3[0]);
//            if (door < 0 || door > 5)
//            {
//              Debug.WriteLine("Fread_rooms: bad door number " + door.ToString() + " on room " + room.VnumRoom.ToString());
//              if (EmlenMud != null)
//              {
//                EmlenMud.LogError("Fread_rooms: bad door number " + door.ToString() + " on room " + room.VnumRoom.ToString(), null, LogType.WorldError);
//              }
//              continue;
//            }
//            int tmp = Convert.ToInt32(rgspl3[1]);
//            if (tmp != 0)
//            {
//              if (pexit.D_info == null) pexit.D_info = new DoorData(EmlenMud);
//              pexit.D_info.Rs_flags = (ExitFlags)tmp;
//            }
//            tmp = Convert.ToInt32(rgspl3[2]);
//            if (tmp != 0)
//            {
//              if (pexit.D_info == null) pexit.D_info = new DoorData(EmlenMud);
//              pexit.D_info.Key = tmp;
//            }
//            pexit.Vnum = Convert.ToInt32(rgspl3[3]);
//            int tmp2 = Convert.ToInt32(rgspl3[4]);
//            int tmp3 = Convert.ToInt32(rgspl3[5]);
//            if (tmp != 0 && tmp != 30)
//            {
//              if (pexit.D_info == null) pexit.D_info = new DoorData(EmlenMud);
//              pexit.D_info.Maxstr = tmp2;	// Check ordering here
//              pexit.D_info.Str = tmp3;
//            }
//            string szL4 = sr.ReadLine();
//            while (szL4.Trim() != "~")
//            {
//              if (pexit.D_info == null) pexit.D_info = new DoorData(EmlenMud);
//              pexit.D_info.Description += szL4 + "\n\r";
//              szL4 = sr.ReadLine();
//            }

//            string szL5 = sr.ReadLine().Trim().TrimEnd('~');
//            if (pexit.D_info == null) pexit.D_info = new DoorData(EmlenMud);
//            pexit.D_info.Keyword = szL5;
//            room.Exits[door] = pexit;
//            continue;
//          }


//          //KEY ("NJAO", currentRoom.img[0], fread_number(fp));
//          //KEY ("NJAT", currentRoom.img[1], fread_number(fp));
//          //KEY ("NAP", currentRoom.a, fread_number (fp));
//          //KEY ("NAC", currentRoom.c, fread_number (fp));
//          if (szWord == "Name")
//          {
//            room.Name = rgsz[1].TrimEnd('~');
//            continue;
//          }
//          if (szWord == "NTPS")
//          {
//            int t = Convert.ToInt32(rgsz[1]);
//            room.TravelPoints = t;
//            continue;
//          }

//          if (room.MoreRoomInfo == null) room.MoreRoomInfo = new DynamicRoomDetails();

//          if (szWord == "Mlt")
//          {
//            continue;
//          }
//          if (szWord == "Mor")
//          {
//            continue;
//          }
//          if (szWord == "Mcts")
//          {
//            continue;
//          }

//          if (szWord == "Move_dir")
//          {
//            room.AutoMoveDirection = (GeneralDirection) Convert.ToInt32(rgsz[1]);
//            continue;
//          }

//          if (szWord == "Movemess")
//          {
//            string szL5 = sr.ReadLine();
//            room.Move_message = szL5.Trim().TrimEnd('~');
//            while (szL5.Trim() != "~" && szL5.Trim().EndsWith("~") == false)
//            {
//              room.Move_message += szL5 + "\n\r";
//              szL5 = sr.ReadLine();
//            }
//            continue;
//          }
//          if (szWord == "Mdescript")
//          {
//            string szL5 = sr.ReadLine();
//            while (szL5.Trim() != "~")
//            {
//              szL5 = sr.ReadLine();
//            }
//            continue;
//          }


//          if (szWord == "Flags") { room.RoomFlagsSet = (RoomFlags)Convert.ToInt32(rgsz[1]); continue; }
//          if (szWord == "Flags2")
//          {
//            Int64 i64 = Convert.ToInt64(rgsz[1]);
//            Int64 i64Sh = i64 << 32;
//            room.RoomFlagsSet |= (RoomFlags)i64Sh;
//            continue;
//          }

//          if (szWord == "Shd")
//          {
//            room.RoomFlagsSet |= (Convert.ToInt32(rgsz[1]) == 1) ? RoomFlags.IS_SHADED : 0;
//            continue;
//          }
//          if (szWord == "Sector")
//          {
//            room.SectorTypeSet = (SectorType)Convert.ToInt32(rgsz[1]);
//            continue;
//          }
//        }
//      }
//    }

//    public void WriteOldRoomFormat(System.IO.StreamWriter sw, int vnum, RoomData room, AreaData area)
//    {
//      char[] delim = new char[3];
//      delim[0] = ' ';
//      delim[1] = '\r';
//      delim[2] = '\n';

//      sw.WriteLine("#" + vnum.ToString());
//      sw.WriteLine("Name " + room.Name + "~");
//      sw.WriteLine("Descr");
//      sw.WriteLine(room.RoomDescription);
//      sw.WriteLine("~");

//      if (room.ExtraRoomDescriptions.Count > 0)
//      {
//        for (int n = 0; n < room.ExtraRoomDescriptions.Count; n++)
//        {
//          ExtraDescriptionData ed = (ExtraDescriptionData)room.ExtraRoomDescriptions[n];
//          sw.WriteLine("ExtraDescr " + ed.Keyword + "~");
//          sw.WriteLine(ed.Description);
//          sw.WriteLine("~");
//        }
//      }

//      if (room.TravelPoints > 0) sw.WriteLine("NTPS " + room.TravelPoints.ToString());
//      if (room.AutoMoveDirection > 0) sw.WriteLine("Move_dir " + room.AutoMoveDirection.ToString());
//      if (!StaticHelpers.Empty(room.Move_message))
//      {
//        sw.WriteLine("Movemess");
//        sw.WriteLine(room.Move_message + "~");
//      }
//      Int64 flags = ((((Int64)room.RoomFlagsSet) << 32) >> 32);
//      Int64 flags2 = (((Int64)room.RoomFlagsSet) >> 32);
//      sw.WriteLine("Flags " + flags.ToString());
//      sw.WriteLine("Flags2 " + flags2.ToString());
//      if ((room.RoomFlagsSet & RoomFlags.IS_SHADED) != 0) sw.WriteLine("Shd 1");
//      sw.WriteLine("Sector " + ((int)room.SectorTypeSet).ToString());

//      for (int n = 0; n < room.Resets.Count; n++)
//      {
//        ResetData reset = (ResetData)room.Resets[n];
//        sw.WriteLine("Reset " + reset.Command.ToString() + " " + reset.Rs_vnum.ToString() + " " +
//          reset.Loc.ToString() + " " + reset.Percent.ToString());
//      }

//      for (int n = 0; n < (int)GeneralDirection.Max; n++)
//      {
//        ExitData ex = (ExitData)room.Exits[n];
//        if (ex != null)
//        {
//          if (ex.D_info != null)
//          {
//            sw.WriteLine("Door " + n.ToString() + " " + ((int)ex.D_info.Rs_flags).ToString() + " " +
//              ex.D_info.Key.ToString() + " " + ex.Vnum.ToString() + " " +
//              ex.D_info.Maxstr.ToString() + " " + ex.D_info.Str.ToString());

//            if (!StaticHelpers.Empty(ex.D_info.Description))
//              sw.WriteLine(ex.D_info.Description);

//            sw.WriteLine("~");

//            sw.WriteLine(ex.D_info.Key + "~");
//          }
//        }
//      }

//      sw.WriteLine("End");
//      sw.WriteLine("");
//    }

//    public void load_rooms_old_format(StreamReader sr, AreaData area)
//    {

//      area.NumberOfRoomsInArea = 0;
//      while (sr.EndOfStream == false)
//      {
//        char letter = ' ';
//        while (letter != '#' && sr.EndOfStream == false) letter = (char)sr.Read();
//        if (letter != '#')
//        {
//          Debug.WriteLine("Error reading rooms: No # found in area " + area.ShortName);
//          if (EmlenMud != null)
//          {
//            EmlenMud.LogError("Error reading rooms: No # found in area " + area.ShortName, null, LogType.WorldError);
//          }
//          return;
//        }
//        string szVnum = sr.ReadLine().Trim();
//        int vnum = Convert.ToInt32(szVnum);
//        if (vnum == 0)	// 0 signals we are done
//          break;


//        if (EmlenMud != null)
//        {
//          if (EmlenMud.WorldTemplates.htRooms.ContainsKey(vnum) == true)
//          {
//            EmlenMud.LogError("Load_rooms: vnum " + szVnum + " duplicated in area " + area.ShortName, null, LogType.WorldError);
//            continue;
//          }
//        }

//        fread_room_old_format(sr, vnum, area);
//        area.NumberOfRoomsInArea++;
//        if (EmlenMud != null)
//        {
//          if (area.OpenToPlayers != 0) EmlenMud.WorldTemplates.nRoomsOpen++;
//          if (vnum > EmlenMud.WorldTemplates.top_room_vnum) EmlenMud.WorldTemplates.top_room_vnum = vnum;
//        }                
//      }
//    }

//    public void save_rooms_old_format(System.IO.StreamWriter sw, AreaData area)
//    {
//      // Do nothing.. this is done in the area file
//    }

//  }
//}
